﻿@using MvcContrib.UI.Grid
@using XRM.Security.WebUI.Helpers
@model List<XRM.Security.WebUI.Models.Department.DepartmentListItem>
@{
    ViewBag.Title = "Струрктурные подразделения";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@section scripts
{
    @Scripts.Render("~/bundles/gridlibrary")
}

<div>
    @if (Permission.AllowDepartmentEdit())
    {
    <p>
        @Html.ActionLink("Добавить подразделение", "Edit", "Department", null, new { @class = "btn btn-large btn-primary" })
    </p>
    }

    @{
        if(Model.Any())
        {
        Html.Grid(Model).Columns(column =>
            {
                column.For(item => Html.ActionLink(item.Name, "Edit", "Department", new { id = item.Id }, null)).Named("Наименование");
                column.Custom(@<text>
                                   @foreach(var addr in item.Addresses)
                                   {
                                    <div>@addr</div>   
                                   }
                               </text>).Named("Адрес");
                column.For(item => item.UsersCount).Named("Количество пользователей").Attributes(width => "220px");
                
                if (Permission.AllowDepartmentEdit())
                {
                    column.Custom(
                        @<text>
                            @if (item.CanDelete)
                            {
                                @Html.Partial("_GridActionsPartial", new ViewDataDictionary { { "Controller", "Department" }, { "Id", item.Id } })
                            }
                        </text>)
                          .Named( "<i class='fa fa-trash-o fa-action'></i>" ).Attributes( @class => "single-action" );
                }

            }).Attributes(@class => "table table-striped table-bordered", id => "table_id")
              .Render();
        }
    } 
</div>

<script type="text/javascript">
    $(document).ready(function () {
        @if (Model.Any())
        {
            if (Permission.AllowDepartmentEdit())
            {
            <text>
            $('#table_id').dataTable({
                "columnDefs": [{ "orderDataType": "numeric", targets: 2 }, { 'bSortable': false, 'aTargets': [1, 3] }],
                paging: false,
                info: false,
                bFilter: false
            });
            </text>
            }
            else
            {
            <text>
            $('#table_id').dataTable({
                    "columnDefs": [{ "orderDataType": "numeric", targets: 2 }, { 'bSortable': false, 'aTargets': [1] }],
                    paging: false,
                    info: false,
                    bFilter: false
            });
            </text>
            }
        }
        });
</script>